﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tulde.Windows.Amazon
{
    public enum ServiceNames
    {
        AWSECommerceService
    }
    public enum Operations
    {
        ItemSearch
    }
    public enum SearchIndex
    {
        Music,
        Books
    }
    public enum ResponseGroup
    {
        Small,
        Medium,
        Large
    }
    public  class ServiceRequestParameters
    {
        protected T GetAsType<T>(string parameter, Func<string, T> parser = null)
        {
            if (parser == null)
            {
                try
                {
                    return (T)Convert.ChangeType(parameter, typeof(T));
                }
                catch
                {
                    return default(T);
                }
            }
            else
            {
                return parser(parameter);
            }
        }
        private Dictionary<string, string> parameters = new Dictionary<string, string>();
        protected void SetParameter(string key, string value)
        {
            if (this.parameters.ContainsKey(key))
            {
                this.parameters[key] = value;
            }
            else
            {
                this.parameters.Add(key, value);
            }
        }
        public IEnumerable<string> Keys
        {
            get
            {
                return this.parameters.Keys.AsEnumerable();
            }
        }
        public string Remove(string key)
        {
            if (this.parameters.ContainsKey(key))
            {
                string r = this.parameters[key];
                this.parameters.Remove(key);
                return r;
            }
            else
            {
                return null;
            }
        }
        public string GetParameter(string key)
        {
            if (this.parameters.ContainsKey(key))
            {
                return this.parameters[key];
            }
            else
            {
                return null;
            }
        }

        public ServiceNames Service
        {
            get
            {
                return (ServiceNames)Enum.Parse(typeof(ServiceNames), this.GetParameter(Constants.Service));
            }
            set
            {
                this.SetParameter(Constants.Service, value.ToString());
            }
        } 
        public string Version
        {
            get
            {
                return this.GetParameter(Constants.Version);
            }
            set
            {
                this.SetParameter(Constants.Version, value);
            }
        }
        public Operations Operation
        {
            get
            {
                return (Operations)Enum.Parse(typeof(Operations), this.GetParameter(Constants.Operation));
            }
            set
            {
                this.SetParameter(Constants.Operation, value.ToString());
            }
        }
        public SearchIndex SearchIndex
        {
            get
            {
                return (SearchIndex)Enum.Parse(typeof(SearchIndex), this.GetParameter(Constants.SearchIndex));
            }
            set
            {
                this.SetParameter(Constants.SearchIndex, value.ToString());
            }
        }
        public ResponseGroup ResponseGroup
        {
            get
            {
                return (ResponseGroup)Enum.Parse(typeof(ResponseGroup), this.GetParameter(Constants.ResponseGroup));
            }
            set
            {
                this.SetParameter(Constants.ResponseGroup, value.ToString());
            }
        }

        
        public override string ToString()
        {
            List<string> nvp = new List<string>();

            foreach (string key in this.Keys)
            {
                nvp.Add(string.Format("{0}={1}", key, this.parameters[key]));
            }
            return String.Join("&", nvp.ToArray());
        }


        public ServiceRequestParameters()
        {
            this.Version = Settings.Version;
        }
    }
}
